package com.cedarhd.brzx.common.utils;

import com.cedarhd.brzx.common.exptions.ValidateException;
import org.json.JSONObject;
import javax.validation.*;
import java.util.Set;

/**
 *
 * @author Evan
 * @date 2018/04/27
 */
public class ValidatorUtil {

    private static ValidatorFactory factory = Validation.buildDefaultValidatorFactory();


    public final static String REGEXP_NOTNULL = "^[A-Za-z0-9]+$";

    public static <T> String validate(T t) {
        Validator validator = factory.getValidator();

        Set<ConstraintViolation<T>> constraintViolations = validator.validate(t);


        for (ConstraintViolation<T> constraintViolation : constraintViolations) {
            throw new ValidateException(constraintViolation.getMessage());
        }
        return null;
    }

    public static void checkJsonResp(JSONObject response) {
        if(response == null){
            throw new ValidateException("调用第三方支付出现异常");
        }
    }
}
